package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Brower;
import com.qy.model.Charge;
import com.qy.model.Policy;
import com.qy.model.Unit;
import com.qy.service.BrowerService;
import com.github.pagehelper.PageHelper;
import com.qy.service.ChargeService;
import com.qy.service.PolicyService;
import com.qy.service.UnitService;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/brower")
public class BrowerController {
    @Resource
    private BrowerService browerService;

    @Resource
    private PolicyService policyService;

    @Resource
    private UnitService unitService;

    @Resource
    private ChargeService chargeService;

    @PostMapping("/add")
    public Result add(@RequestBody Brower brower) {
        browerService.save(brower);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        browerService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Brower brower) {
        browerService.update(brower);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Brower brower = browerService.findById(id);
        return ResultGenerator.successResult(brower);
    }

    @GetMapping("/list")
    public Result list(PageBean<Brower> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Brower> list = browerService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //浏览
    @PostMapping("/brower")
    public Result brower(@RequestBody Brower brower) {
        if (brower.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(Brower.class);
        Example.Criteria criteria = condition.createCriteria();
        if (brower.getPolicy_id() != null) {
            Policy policy = policyService.findById(brower.getPolicy_id());
            criteria.andCondition("user_id =" + brower.getUser_id());
            criteria.andCondition("policy_id =" + brower.getPolicy_id());
            List<Brower> browerList = browerService.findByCondition(condition);
            if (browerList.size() == 0) {
                policy.setBrower_number(policy.getBrower_number() + 1);
                policyService.update(policy);
            }
        } else if (brower.getUnit_id() != null) {
            Unit unit = unitService.findById(brower.getUnit_id());
            criteria.andCondition("user_id =" + brower.getUser_id());
            criteria.andCondition("unit_id =" + brower.getUnit_id());
            List<Brower> browerList = browerService.findByCondition(condition);
            if (browerList.size() == 0) {
                unit.setBrower_number(unit.getBrower_number() + 1);
                unitService.update(unit);
            }
        } else if (brower.getCharge_id() != null) {
            Charge charge = chargeService.findById(brower.getCharge_id());
            criteria.andCondition("user_id =" + brower.getUser_id());
            criteria.andCondition("charge_id =" + brower.getCharge_id());
            List<Brower> browerList = browerService.findByCondition(condition);
            if (browerList.size() == 0) {
                charge.setBrower_number(charge.getBrower_number() + 1);
                chargeService.update(charge);
            }
        }
        brower.setAdd_time(DateUtil.getNowTimestamp());
        browerService.save(brower);
        return ResultGenerator.successResult();
    }
}
